package com.crm.common.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 对象工具类
 * 主要用于redis缓存与对象之间的对应转换
 *
 * @author luojie
 * @date 2021/2/1 11:14
 */
public class ObjectUtil {

    /**
     * 将一个map转换为对象
     *
     * @param map
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static <T> T mapToObject(Map<Object, Object> map, Class<T> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = beanClass.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                String propertyName = property.getName();
                Object value = map.get(propertyName);
                if (value != null) {
                    // 如果当前的value值非空，则继续进行判断
                    Class<?>[] parameterTypes = setter.getParameterTypes();
                    Class<?> paramterType = parameterTypes[0];
                    Class<?> valueClass = value.getClass();
                    if (valueClass.equals(Integer.class) && paramterType.equals(Long.class)) {
                        // 如果遇到了Integer转Long的情况
                        value = new Long((Integer) value);
                    }
                    setter.invoke(obj, value);
                }
            }
        }
        return (T) obj;
    }

    /**
     * 将一个对象转换为map
     *
     * @param obj 对象
     * @return 属性名称对应值的格式的map
     * @throws Exception
     */
    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        } else {

            Map<String, Object> map = new HashMap<String, Object>();

            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (key.compareToIgnoreCase("class") == 0) {
                    continue;
                }
                Method getter = property.getReadMethod();
                Object value = getter != null ? getter.invoke(obj) : null;
                map.put(key, value);
            }
            return map;
        }
    }

    /**
     * int转boolean
     *
     * @param integer
     * @return
     */
    public static Boolean intToBoolean(Integer integer) {
        if (integer == null) {
            return Boolean.FALSE;
        } else {
            if (integer.equals(0)) {
                return Boolean.FALSE;
            } else {
                return Boolean.TRUE;
            }
        }
    }

    /**
     * boolean转int
     *
     * @param bool
     * @return
     */
    public static Integer booleanToInteger(Boolean bool) {
        if (bool) {
            return 1;
        } else {
            return 0;
        }
    }
}
